package org.skydiveapp;

import org.skydiveapplibrary.data.Constants;
import org.skydiveapplibrary.repository.SettingsRepository;
import org.skydiveapplibrary.ui.InterfaceManager;
import org.skydiveapplibrary.R;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

public class RigComponentEditActivity extends SherlockActivity
{
	// requests
    private static final int REQUEST_NOTES = 0;

	// data
	private String notes = "";
	private Boolean isEdit = false;
	private Boolean isDualDisplay;
	
	// listeners
	private OnClickListener notesEditListener = new OnClickListener()
	{
		@Override
		public void onClick(View view)
		{		
			Intent intent = new Intent(RigComponentEditActivity.this, NotesEditActivity.class);
            intent.putExtra(Constants.EXTRA_NOTES, notes);
            startActivityForResult(intent, REQUEST_NOTES);
		}
	};
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		SettingsRepository settingsRepository = new SettingsRepository(this);
		if (settingsRepository.getUseLightTheme())
			super.setTheme(R.style.SkydiveAppTheme_Light_Transparent);
		
		super.onCreate(savedInstanceState);
		
        // init ui
        initializeUI();
	}
	
	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent)
    {
            if (requestCode == REQUEST_NOTES && resultCode == RESULT_OK)
            {
                    String notes = intent.getStringExtra(Constants.EXTRA_NOTES);
                    updateNotes(notes);
            }
    }
	
	private void initializeUI()
	{
		// show view
		setContentView(R.layout.rig_component_edit_view);
		
		// set subtitle text
		InterfaceManager.getInstance().setSubtitleText(getSupportActionBar(), 
						getResources().getString(R.string.rig_component_header));
		
		isDualDisplay = this.findViewById(R.id.phoneLayout) == null;
		
		// get data
		Bundle extras = getIntent().getExtras();
		isEdit = (extras != null && extras.getBoolean(Constants.EXTRA_RIG_COMPONENT_IS_EDIT, false));
		String name = (extras != null && extras.containsKey(Constants.EXTRA_RIG_COMPONENT_NAME)) ?
		                extras.getString(Constants.EXTRA_RIG_COMPONENT_NAME) : "";
		String serialNumber = (extras != null && extras.containsKey(Constants.EXTRA_RIG_COMPONENT_SERIAL_NUMBER)) ?
		                extras.getString(Constants.EXTRA_RIG_COMPONENT_SERIAL_NUMBER) : "";
		notes = (extras != null && extras.containsKey(Constants.EXTRA_RIG_COMPONENT_NOTES)) ?
		                extras.getString(Constants.EXTRA_RIG_COMPONENT_NOTES) : "";
		
		// set name
		EditText nField = (EditText)findViewById(R.id.RigComponentNameField);
		nField.setText(name);
		
		// set serial number
		EditText snField = (EditText)findViewById(R.id.RigComponentSerialNumberField);
		snField.setText(serialNumber);
		
		// set notes
		TextView notesField = (TextView)findViewById(R.id.RigComponentNotesField);
		notesField.setOnClickListener(notesEditListener);
		notesField.setText(notes);
	}
	
	public void updateNotes(String notes)
	{
		this.notes = notes;
        
        // update UI
        TextView notesField = (TextView)findViewById(R.id.RigComponentNotesField);
        notesField.setText(notes);
	}
	
    private void save()
    {
		// get name from field
		EditText nField = (EditText)findViewById(R.id.RigComponentNameField);
		String name = nField.getText().toString();
		
		// get serial number from field
		EditText snField = (EditText)findViewById(R.id.RigComponentSerialNumberField);
		String serialNumber = snField.getText().toString();
		
		// set result
		Intent result = new Intent();
		result.putExtra(Constants.EXTRA_RIG_COMPONENT_NAME, name);
		result.putExtra(Constants.EXTRA_RIG_COMPONENT_SERIAL_NUMBER, serialNumber);
		result.putExtra(Constants.EXTRA_RIG_COMPONENT_NOTES, notes);
		
		setResult(RESULT_OK, result);
		
		// finish
		close();
    }
	
	private void delete()
	{
        Intent result = new Intent();
        result.putExtra(Constants.EXTRA_RIG_COMPONENT_DELETE, true);
        setResult(RESULT_OK, result);
        
        // finish
        close();
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.menu_save, menu);
	    
		if (isEdit)
			inflater.inflate(R.menu.menu_delete, menu);
		
		inflater.inflate(R.menu.menu_back, menu);
	    return true;
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu)
    {
    	menu.findItem(R.id.menu_back).setVisible(isDualDisplay);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        int itemId = item.getItemId();
		if (itemId == R.id.menu_save)
		{
			save();
			return true;
		}
		else if (itemId == R.id.menu_back)
		{
			close();
			return true;
		}
		else if (itemId == R.id.menu_delete)
		{
			delete();
			return true;
		}
		else
		{
			return super.onOptionsItemSelected(item);
		}
    }
    
    private void close()
    {
    	finish();
    }
    
    @Override
    public void onBackPressed()
    {
    	save();
    }
}
